/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.samuelwall.materialtaptargetprompt.sample.weight.MaterButton;

import ohos.agp.animation.Animator;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;
import ohos.app.Context;
import uk.co.samuelwall.materialtaptargetprompt.sample.utils.Utils;

/**
 * CustomView
 *
 * @since 2021-06-21
 */
public class CustomView extends DependentLayout implements Component.DrawTask, Animator.StateChangedListener {
    final int disabledBackgroundColor = Color.getIntColor("#FFE2E2E2");
    String beforeBackground;
    boolean isAnimation = false;
    /**
     * isLastTouch
     */
    private boolean isLastTouch = false;

    /**
     * CustomView
     *
     * @param context context
     */
    public CustomView(Context context) {
        this(context, null);
    }

    /**
     * CustomView
     *
     * @param context context
     * @param attrs attrs
     */
    public CustomView(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    /**
     * CustomView
     *
     * @param context context
     * @param attrs attrs
     * @param defStyleAttr defStyleAttr
     */
    public CustomView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        addDrawTask(this);
        createAnimatorProperty().setStateChangedListener(this);
    }

    @Override
    public void setEnabled(boolean isEnabled) {
        super.setEnabled(isEnabled);
        ShapeElement shapeElement = new ShapeElement();
        if (isEnabled) {
            shapeElement.setRgbColor(new RgbColor(Color.getIntColor(Utils.rgba(beforeBackground))));
        } else {
            shapeElement.setRgbColor(new RgbColor(disabledBackgroundColor));
        }
        setBackground(shapeElement);
        invalidate();
    }

    public boolean isLastTouch() {
        return isLastTouch;
    }

    public void setLastTouch(boolean isLast) {
        isLastTouch = isLast;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (isAnimation) {
            getContext().getUITaskDispatcher().asyncDispatch(this::invalidate);
        }
    }

    @Override
    public void onStart(Animator animator) {
        isAnimation = true;
    }

    @Override
    public void onStop(Animator animator) {
        isAnimation = false;
    }

    @Override
    public void onCancel(Animator animator) {
    }

    @Override
    public void onEnd(Animator animator) {
    }

    @Override
    public void onPause(Animator animator) {
    }

    @Override
    public void onResume(Animator animator) {
    }
}
